export class Bullet {
  name = "";
  setName(name) {
    this.name = name;
  }

  image = new Image();
  setImage(src) {
    this.image.src = src;
  }

  width = 0;
  setWidth(value) {
    this.width = value;
  }

  height = 0;
  setHeight(value) {
    this.height = value;
  }

  speed = 0;
  setMaxSpeed(speed) {
    this.speed = speed;
  }

  position = [0, 0];
  setPosition(x, y) {
    this.position = [x, y];
  }

  isHit(bullet){
    const my = {
      minx: this.position[0],
      maxx: this.position[0] + this.width,
      miny: this.position[1],
      maxy: this.position[1] + this.height,
    };

    const blt = {
      minx: bullet.position[0],
      maxx: bullet.position[0] + bullet.width,
      miny: bullet.position[1],
      maxy: bullet.position[1] + bullet.height,
    };

    if (my.maxx < blt.minx) return false;
    if (my.minx > blt.maxx) return false;
    if (my.maxy < blt.miny) return false;
    if (my.miny > blt.maxy) return false;

    return true;
  }

  onDestoryCallback = []
  destory() {
    this.setPosition(innerWidth * 2, -100);
    this.onDestoryCallback?.forEach(fn => fn())
  }
  ondestory(callback){
    this.onDestoryCallback.push(callback);
  }
  removeDestory(callback){
    const index = this.onDestoryCallback?.findIndex(item => item === callback)
    if (index > -1){
      this.onDestoryCallback.splice(index, 1);
    }
  }
}
